package nadav.tasher.lightool.info;

import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.bundle.IBundleManager;
import ohos.net.NetManager;
import ohos.rpc.RemoteException;
import ohos.workscheduler.WorkInfo;
import ohos.workscheduler.WorkScheduler;
import java.util.Optional;

public class Device {
    public static boolean isOnline(Context c){
        NetManager manager = NetManager.getInstance(c);
        return (manager!=null)&&manager.getAppNet()!=null;
    }


    public static boolean isInstalled(Context con,String packageName){
        try {
            con.getBundleManager().getBundleInfo(packageName,IBundleManager.GET_BUNDLE_WITH_ABILITIES);
            return true;
        } catch (RemoteException e) {
            e.printStackTrace();
            return false;
        }
    }
    public static int getVersionCode(Context con, String packageName){
        try {
            return con.getBundleManager().getBundleInfo(packageName,IBundleManager.GET_BUNDLE_WITH_ABILITIES).getVersionCode();
        } catch (RemoteException e) {
            e.printStackTrace();
            return 0;
        }
    }
    public static String getVersionName(Context con, String packageName){
        try {
            return con.getBundleManager().getBundleInfo(packageName,IBundleManager.GET_BUNDLE_WITH_ABILITIES).getVersionName();
        } catch (RemoteException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static boolean isJobServiceScheduled(Context context, int id) throws RemoteException {
        WorkScheduler workScheduler=new WorkScheduler() {
            @Override
            public void onWorkStart(WorkInfo workInfo) {
            }

            @Override
            public void onWorkStop(WorkInfo workInfo) {
            }
        };
        boolean hasBeenScheduled = false;
        if (workScheduler!=null){
            for (WorkInfo workInfo:WorkScheduler.obtainAllWorks()){
                if (workInfo.getCurrentWorkID()==id){
                    hasBeenScheduled=true;
                    break;
                }
            }
            return hasBeenScheduled;
        }
        else {
            return false;
        }
    }
    public static float screenX(Context con){
        DisplayManager displayManager=DisplayManager.getInstance();
        if (displayManager!=null) {
            Optional<Display> defaultDisplay = displayManager.getDefaultDisplay(con);
            Point point = new Point();
            defaultDisplay.get().getSize(point);
            return point.getPointX();
        }
         return 0;
    }
    public static float screenY(Context con){
//        DisplayManager displayManager=DisplayManager.getInstance();
//        if (displayManager!=null) {
//            Optional<Display> defaultDisplay = displayManager.getDefaultDisplay(con);
//            Point point = new Point();
//            defaultDisplay.get().getSize(point);
//            return point.getPointY();
//        }
    //    return 0;
        Point p = new Point();
        DisplayManager.getInstance().getDefaultDisplay(con).get().getSize(p);
        return (int)p.getPointY();
    }
}
